{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Using Interact" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `interact` function (`ipywidgets.interact`) automatically creates user interface (UI) controls for exploring code and data interactively. It is the easiest way to get started using IPython's widgets." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": ["remove-cell"] }, "outputs": [], "source": [ "# Imports for JupyterLite\n", "%pip install -q ipywidgets" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from ipywidgets import interact, interactive, fixed, interact_manual\n", "import ipywidgets as widgets" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic `interact`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "At the most basic level, `interact` autogenerates UI controls for function arguments, and then calls the function with those arguments when you manipulate the controls interactively. To use `interact`, you need to define a function that you want to explore. Here is a function that returns its only argument `x`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def f(x):\n", " return x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When you pass this function as the first argument to `interact` along with an integer keyword argument (`x=10`), a slider is generated and bound to the function parameter." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "interact(f, x=10);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When you move the slider, the function is called, and its return value is printed.\n", "\n", "If you pass `True` or `False`, `interact` will generate a checkbox:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "interact(f, x=True);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you pass a string, `interact` will generate a text box." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "interact(f, x='Hi there!');" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`interact` can also be used as a decorator. This allows you to define a function and interact with it in a single shot. As this example shows, `interact` also works with functions that have multiple arguments." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "@interact(x=True, y=1.0)\n", "def g(x, y):\n", " return (x, y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Fixing arguments using `fixed`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are times when you may want to explore a function using `interact`, but fix one or more of its arguments to specific values. This can be accomplished by wrapping values with the `fixed` function." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def h(p, q):\n", " return (p, q)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When we call `interact`, we pass `fixed(20)` for q to hold it fixed at a value of `20`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "interact(h, p=5, q=fixed(20));" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that a slider is only produced for `p` as the value of `q` is fixed." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Widget abbreviations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When you pass an integer-valued keyword argument of `10` (`x=10`) to `interact`, it generates an integer-valued slider control with a range of `[-10,+3*10]`. In this case, `10` is an *abbreviation* for an actual slider widget:\n", "\n", "```python\n", "IntSlider(min=-10, max=30, step=1, value=10)\n", "```\n", "\n", "In fact, we can get the same result if we pass this `IntSlider` as the keyword argument for `x`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "interact(f, x=widgets.IntSlider(min=-10, max=30, step=1, value=10));" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following table gives an overview of different argument types, and how they map to interactive controls:\n", "\n", "
Keyword argument | Widget |
`True` or `False` | Checkbox |
`'Hi there'` | Text |
`value` or `(min,max)` or `(min,max,step)` if integers are passed | IntSlider |
`value` or `(min,max)` or `(min,max,step)` if floats are passed | FloatSlider |
`['orange','apple']` or `[('one', 1), ('two', 2)] | Dropdown |